var e, t = require("../../@babel/runtime/helpers/interopRequireDefault"), a = t(require("../../@babel/runtime/regenerator")), r = require("../../@babel/runtime/helpers/asyncToGenerator"), i = t(require("../../utils/WxValidate")), n = t(require("../../utils/api")), s = getApp();

Page({
    data: {
        statusBarHeight: s.globalData.statusBarHeight,
        form: {
            username: "",
            phone: "",
            companyName: "",
            companyId: "",
            authCode: "",
            isParty: 1,
            partyName: "党员",
            partyBranchId: "",
            partyBranchName: ""
        },
        index: 0,
        partyIndex: 0,
        partyBranchIndex: 0,
        companyList: [],
        partyList: [],
        authCodeVisible: !1,
        isParty: [ {
            id: 0,
            name: "非党员"
        }, {
            id: 1,
            name: "党员"
        } ],
        logined: !1
    },
    debounce: function(e, t) {
        var a = arguments.length > 2 && void 0 !== arguments[2] && arguments[2], r = null;
        return function() {
            var i = arguments, n = this;
            r && clearTimeout(r), a ? (r || e.apply(this, arguments), r = setTimeout(function() {
                r = null;
            }, t)) : r = setTimeout(function() {
                e.apply(n, i);
            }, t);
        };
    },
    initValidate: function() {
        this.WxValidate = new i.default({
            username: {
                required: !0,
                username: !0
            },
            phone: {
                required: !0,
                userPhone: !0
            },
            companyName: {
                required: !0
            }
        }, {
            username: {
                required: "请输入真实姓名"
            },
            phone: {
                required: "请输入手机号码",
                userPhone: "请输入正确的手机号"
            },
            companyName: {
                required: "请选择公司"
            }
        }), this.WxValidate.addMethod("username", function(e, t) {
            return e.replace(/(^\s+)|(\s+$)/g, "").match(/^[0-9a-zA-Z\u4e00-\u9fa5]+$/);
        }, "姓名仅支持汉字、英文、数字"), this.WxValidate.addMethod("userPhone", function(e, t) {
            return e.replace(/(^\s+)|(\s+$)/g, "").match(/^(\d{11})$/);
        }, "手机号仅支持11位");
    },
    onShow: (e = r(a.default.mark(function e(t) {
        var r, i;
        return a.default.wrap(function(e) {
            for (;;) switch (e.prev = e.next) {
              case 0:
                return this.initValidate(), e.next = 3, n.default.companyList({});

              case 3:
                return r = e.sent, e.next = 6, n.default.partyList({});

              case 6:
                i = e.sent, this.setData({
                    companyList: r.result,
                    partyList: i.result[0].children
                }), this.verifyDebounce = this.debounce(this.verify, 600, !1);

              case 9:
              case "end":
                return e.stop();
            }
        }, e, this);
    })), function(t) {
        return e.apply(this, arguments);
    }),
    pickerChange: function(e) {
        this.setData({
            index: e.detail.value,
            "form.companyName": this.data.companyList[e.detail.value].name,
            "form.companyId": this.data.companyList[e.detail.value].id
        });
    },
    pickerPartyChange: function(e) {
        this.setData({
            partyIndex: e.detail.value,
            "form.isParty": this.data.isParty[e.detail.value].id,
            "form.partyName": this.data.isParty[e.detail.value].name
        });
    },
    pickerPartyBranchChange: function(e) {
        this.setData({
            partyBranchIndex: e.detail.value,
            "form.partyBranchId": this.data.partyList[e.detail.value].id,
            "form.partyBranchName": this.data.partyList[e.detail.value].name
        });
    },
    handleLogin: function(e) {
        var t = e.detail.value;
        if (!this.WxValidate.checkForm(t)) {
            var a = this.WxValidate.errorList[0];
            return wx.showToast({
                title: a.msg,
                icon: "none",
                duration: 1e3
            }), !1;
        }
        return 7 != this.data.form.companyId || 1 != this.data.form.isParty || this.data.form.partyBranchId ? this.data.authCodeVisible && !t.authCode ? (wx.showToast({
            title: "请输入验证码",
            icon: "none",
            duration: 1e3
        }), !1) : (this.setData({
            form: Object.assign(this.data.form, t)
        }), void wx.redirectTo({
            url: "/pages/loginAuthorize/loginAuthorize?param=" + JSON.stringify(this.data.form)
        })) : (wx.showToast({
            title: "请选择党支部",
            icon: "none",
            duration: 1e3
        }), !1);
    },
    verify: function(e) {
        var t = this;
        return r(a.default.mark(function r() {
            var i;
            return a.default.wrap(function(a) {
                for (;;) switch (a.prev = a.next) {
                  case 0:
                    if (11 !== e.detail.value.length) {
                        a.next = 11;
                        break;
                    }
                    return a.prev = 1, a.next = 4, n.default.verifyPhoneNum({
                        phone: e.detail.value
                    });

                  case 4:
                    1 === (i = a.sent).result.type ? t.setData({
                        authCodeVisible: !1
                    }) : 2 === i.result.type ? t.setData({
                        authCodeVisible: !0
                    }) : 3 === i.result.type && wx.showToast({
                        title: i.result.msg,
                        icon: "none",
                        duration: 3e3
                    }), a.next = 11;
                    break;

                  case 8:
                    a.prev = 8, a.t0 = a.catch(1), console.log(a.t0);

                  case 11:
                  case "end":
                    return a.stop();
                }
            }, r, null, [ [ 1, 8 ] ]);
        }))();
    }
});